﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace HTCCoverImporter
{
    class FormatFinder
    {
        public static void OddOneOut(string dir, List<string> lstFiles)
        {
            string[] dirs = Directory.GetDirectories(dir);
            bool found = false;

            for (int i = 0; i <= dirs.Length - 1; i++)
            {
                string[] files = Directory.GetFiles(dirs[i]);

                for (int j = 0; j <= files.Length - 1; j++)
                {
                    using (Image img = Image.FromFile(files[j]))
                    {
                        if (img.PixelFormat != PixelFormat.Format24bppRgb)
                        {
                            if (!lstFiles.Contains(files[j].Split('\\')[8]))
                                lstFiles.Add(files[j].Split('\\')[8]);

                            using (Bitmap b = new Bitmap(img))
                            {
                                b.MakeTransparent(Color.White);

                                //Save The Original One With Tmp Extension
                                b.Save(Path.GetDirectoryName(files[j] + "/" + Path.GetFileName(files[j])).Replace(".jpg", ".tmp"), ImageFormat.Jpeg);
                            }

                            found = true;
                        }
                    }

                    if (found)
                    {
                        File.Delete(files[j]);
                        File.Move(Path.GetDirectoryName(files[j] + "/" + Path.GetFileName(files[j])).Replace(".jpg", ".tmp"), files[j]);
                        found = false;
                    }
                }
            }
        }        
    }
}
